trigger setmanagerformd on marketdynamic__c (before update,before insert) {
    list<id> idl=new list<id>();
    list<marketdynamic__c> mdl=new list<marketdynamic__c>();
    list<user> u=new list<user>();
    list<id> uid=new list<id>();
    
     for(marketdynamic__c md:trigger.new){
         idl.add(md.id);
         uid.add(md.ownerid);
     }
     
    u=[select id,managerid from user where id in:uid];
    
    if(system.trigger.isupdate){
    for(marketdynamic__c mdt:trigger.new){
        if(system.trigger.oldmap.get(mdt.id).ownerid != mdt.ownerid){
                for(user ul:u){
                    if(ul.id==mdt.ownerid){
                        mdt.manager__c=ul.managerid;
                    }
                
              }  
            }
        }
    }else if(system.trigger.isinsert){
        for(marketdynamic__c mdt:trigger.new){
                for(user ul:u){
                    if(ul.id==mdt.ownerid){
                       mdt.manager__c=ul.managerid;

                }
              }  
            }
        }
    
     
     /*  for(marketdynamic__c md:trigger.new){
        if(system.trigger.isupdate){
            if(system.trigger.oldmap.get(md.id).ownerid != md.ownerid){
                user u=[select id,managerid from user where id=: md.OwnerId];
                marketdynamic__c hl=[select id,manager__c from marketdynamic__c where id=:md.id];
                    if(u.managerid<>null){
                        marketdynamic__c h=hl;
                        h.manager__c=u.managerid;
                        update h;
                        }
                    }
             }else if(system.trigger.isinsert){
                     user u=[select id,managerid from user where id=: md.OwnerId];
                marketdynamic__c hl=[select id,manager__c from marketdynamic__c where id=:md.id];
                    if(u.managerid<>null){
                        marketdynamic__c h=hl;
                        h.manager__c=u.managerid;
                        update h;
                        }
             }
        }*/
}